package com.gongpingjialoan.view;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.gongpingjialoan.R;


public class ProgressView extends View {
    //用于画背景
    Paint mPaint;
    int progresswidth;
    float mPercent;
    int startAngle;
    int maxAngle;
    float progress;
    boolean showBackground = true;

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.text_blue));
        mPaint.setAntiAlias(true);
        float scale = context.getResources().getDisplayMetrics().density;
        progresswidth = (int) (scale * 300 / (float) 502 * 15 + 0.5f);
        mPaint.setStrokeWidth(progresswidth);
        progresswidth = progresswidth / 2 + 2;
        mPaint.setStyle(Paint.Style.STROKE);
        maxAngle = 360;

    }

    public void setStrokeWidth(int StrokeWidth) {
        mPaint.setStrokeWidth(StrokeWidth);
        progresswidth = StrokeWidth / 2 + 2;
        invalidate();
    }

    public void setColor(int color) {
        mPaint.setColor(color);
        invalidate();
    }

    public void setStartAngle(int startAngle) {
        this.startAngle = startAngle;
        invalidate();
    }

    public void setMaxAngle(int maxAngle) {
        this.maxAngle = maxAngle;
        invalidate();
    }

    public void setBackgroundVisible(boolean showBackground){
        this.showBackground = showBackground;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画弧线 
        RectF oval = new RectF(progresswidth, progresswidth, getWidth() - progresswidth, getHeight() - progresswidth);
        mPaint.setStyle(Paint.Style.STROKE);

        if (showBackground){
            mPaint.setColor(getResources().getColor(R.color.progress_bg_color));
            canvas.drawArc(oval, this.startAngle, this.maxAngle, false, mPaint);
        }

        mPaint.setColor(getResources().getColor(R.color.progress_color));
        canvas.drawArc(oval, this.startAngle, mPercent, false, mPaint);

    }

    public void setProgress(float progress) {
        this.progress = progress;
        mPercent = (float) progress * maxAngle;
        invalidate();
    }

    public void animProgress(float progress) {
        ObjectAnimator.ofFloat(this, "progress", this.progress, progress).setDuration(2000).start();
    }

    public float getProgress() {
        return this.progress;
    }

}
